package com.sn.author.utils;

import com.sn.author.common.exception.BadRequestException;
import com.sn.author.common.sdk.Client;
import com.sn.author.common.sdk.client.YKClient;
import com.sn.author.common.sdk.entity.AbstractResult;
import com.sn.author.common.sdk.request.*;
import com.sn.author.common.sdk.response.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class SdkProcessService {

    public static Client getClient(){
        return new YKClient();
    }

    /**
     * 调用抖音<注册> api
     * @param num 注册个数
     */
    public AbstractResult executeRegisterSdk(Integer num){
        RegisterRequest request = new RegisterRequest();
        request.setRegNum(num);

        try {
            RegisterResponse registerResponse =  getClient().execute(request);
            if(registerResponse == null || !registerResponse.getSuccess()){
                throw new BadRequestException("调用抖音 注册 api返回结果错误");
            }
            return registerResponse.getData();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BadRequestException("调用抖音 注册 api失败 错误信息:" + e.getMessage());
        }
    }

    public AttentionResponse executeAttentionSdk(String urlParam, String header, String phone, String videoUrl,String areaCode){
        AttentionRequest request = new AttentionRequest();
        request.setUrlParam(urlParam);
        request.setHeader(header);
        request.setPhone(phone);
        request.setUrl(videoUrl);
        request.setAreaCode(Long.parseLong(areaCode));

        try {
            return getClient().execute(request);
        } catch (Exception e) {
            log.error("调用抖音 关注 api失败 错误信息:{}",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public LikeResponse executeLikeSdk(String urlParam, String header, String phone, String videoUrl,String areaCode){
        LikeRequest request = new LikeRequest();
        request.setUrlParam(urlParam);
        request.setHeader(header);
        request.setPhone(phone);
        request.setVideoUrl(videoUrl);
        request.setAreaCode(Long.parseLong(areaCode));

        try {
            return getClient().execute(request);
        } catch (Exception e) {
            log.error("调用抖音 点赞 API出现异常 e:{}",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public IMResponse executeIMSdk(String phone,String password,String text,String toUserIdStr,Integer type,String deviceId){
        IMRequest request = new IMRequest();

        List<IMRequest.MessageParam> pList = new ArrayList<>();
        String[] toUserArray = toUserIdStr.split(",");
        for (String userId : toUserArray) {
            IMRequest.MessageParam param = new IMRequest.MessageParam();
            param.setText(text);
            param.setType(type);
            param.setUserId(userId);
            pList.add(param);
        }

        request.setPList(pList);
        request.setPhone(phone);
        request.setPassword(password);
        request.setDeviceId(deviceId);

        try {
            return getClient().execute(request);
        }catch (Exception e){
            log.error("调用抖音 IM 私信 API出现异常 e:{}",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 调用抖音 私信 (名片) 接口
     */
    public IMResponse executeCardIMSdk(String phone,String password,String toUserIdStr,String deviceId,String title,String url,String image,String desc){
        IMCardRequest request = new IMCardRequest();

        List<IMCardRequest.MessageParam> pList = new ArrayList<>();
        String[] toUserArray = toUserIdStr.split(",");
        for (String userId : toUserArray) {
            IMCardRequest.MessageParam param = new IMCardRequest.MessageParam();
            param.setTitle(title);
            param.setDesc(desc);
            param.setImage(image);
            param.setUrl(url);
            param.setUserId(userId);
            pList.add(param);
        }
        request.setPList(pList);
        request.setPhone(phone);
        request.setPassword(password);
        request.setDeviceId(deviceId);

        try {
            return getClient().execute(request);
        }catch (Exception e){
            log.error("调用抖音 IM CARD 私信 API出现异常 e:{}",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public VideoInfoResponse executeGetVideoInfoSdk(String url){
        VideoInfoRequest request = new VideoInfoRequest();
        request.setUrl(url);
        try {
            return getClient().execute(request);
        }catch (Exception e){
            log.error("调用抖音 获取视频信息 API出现异常 e:{}",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public CommentResponse executeCommentSdk(String content,String urlParam,String header,String videoUrl,String areaCode){
        CommentRequest request = new CommentRequest();
        request.setComment(content);
        request.setHeader(header);
        request.setUrl(videoUrl);
        request.setUrlParam(urlParam);
        request.setAreaCode(Long.parseLong(areaCode));

        try {
            return getClient().execute(request);
        }catch (Exception e){
            log.error("调用抖音 评论 API出现异常 e:{}",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public CollectionResponse executeCollection(Integer num,String areaCode,Long taskId,String header,String urlParam){
        CollectionRequest request = new CollectionRequest();
        request.setNum(num);
        request.setAreaCode(Long.parseLong(areaCode));
        request.setTaskId(taskId);
        request.setHeader(header);
        request.setUrlParam(urlParam);
        try {
            return getClient().execute(request);
        }catch (Exception e){
            log.error("调用抖音 数据采集 API出现异常 e:{}",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public LoginVerifyResponse executeLoginVerify(Integer code){
        LoginVerifyRequest request = new LoginVerifyRequest();
        request.setCode(code);
        try {
            return getClient().execute(request);
        }catch (Exception e){
            log.error("调用抖音 发送验证码 API出现异常 e:{}",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}
